/**
 * [高德地图操作基类]
 * @author [leixin]
 * @date [2015-04-01]
 * @remark [依赖高德JS API]
 * @param {[type]} elementName [地图容器id]
 * @param {[type]} AMap        [高德地图AMap对象]
 * @param {[type]} $           [AppFramework对象]
 */
function MapBase(){
	this.mapObj = null;
}

/**
 * [加载地图]
 * @param  {[type]} options [description]
 * @return {[type]}         [description]
 */
MapBase.prototype.initialize = function(options){
	options = options || {};
	var x = options.x || 113.358169;
	var y = options.y || 23.135021;
	var initZoom = options.initZoom || 16;
	var maxZoom = options.maxZoom || 19;
	var minZoom = options.minZoom || 8;
	this.mapObj = new AMap.Map(options.elementId, {
        view: new AMap.View2D({
            center: new AMap.LngLat(x, y),
            zoom: initZoom
        }),
        zooms: [minZoom, maxZoom]
    });
};

/**
 * [加载地图比例尺]
 * @param {[type]} options [description]
 */
MapBase.prototype.addScalePlugin = function (options) {
	var enhance = options.enhance || true;
	var that = this;
	this.mapObj.plugin(["AMap.Scale"], function () {
        var scale = new AMap.Scale();
        that.mapObj.addControl(scale);

        that.fixMapLayout();

        // 使用css实现
        // if(enhance){
        // 	$('.amap-controls').get(0).style.position = 'absolute';
        //     $('.amap-controls').get(0).style.bottom = '2rem';
        //     $('.amap-controls').get(0).style.left = '2rem';
        //     $('.amap-scale-edgeleft').get(0).style.borderColor = 'black';
        //     $('.amap-scale-edgeright').get(0).style.borderColor = 'black';
        //     $('.amap-scale-middle').get(0).style.borderColor = 'black';
        // }
	});
};

/**
 * [创建图标]
 * @param  {[type]} options [description]
 * @return {[type]}         [description]
 */
MapBase.prototype.createIconMarker = function(options){
	var x = options.x;
	var y = options.y;
	var src = options.src;
	var imageWidth = options.imageWidth || 36;
	var imageHeight = options.imageHeight || 36;
	var markerWidth = options.markerWidth || 36;
	var markerHeight = options.markerHeight || 36;
	var offsetX = options.offsetX || -10;
	var offsetY = options.offsetY || -34;
	var topWhenMouseOver = options.topWhenMouseOver || true;
	var extData = options.extData || {};

    var marker = new AMap.Marker({
        icon: new AMap.Icon({
            image: src,
            imageSize: new AMap.Size(imageWidth, imageHeight),
            size: new AMap.Size(markerWidth, markerHeight)
        }),
        position: new AMap.LngLat(x, y),
        offset: new AMap.Pixel(offsetX, offsetY),
        topWhenMouseOver: topWhenMouseOver,
        extData: extData
    });

    return marker;
}

/**
 * [创建圆]
 * @param  {[type]} options [description]
 * @return {[type]}         [description]
 */
MapBase.prototype.createCircle = function(options){
	var x = options.x;
	var y = options.y;
	var radius = options.radius || 50;
	var strokeColor = options.strokeColor || '#9BCAE8';
	var strokeWeight = options.strokeWeight || 1;
	var fillColor = options.fillColor || '#E1EAEF';
	var fillOpacity = options.fillOpacity || 0.4;

    var circle = new AMap.Circle({
        center: new AMap.LngLat(x, y),
        radius: radius,
        strokeColor: strokeColor,
        strokeWeight: strokeWeight,
        fillColor: fillColor,
        fillOpacity: fillOpacity
    });

    return circle;
};

/**
 * [fixMapLayout 地图不好用的时候就用这函数]
 * @return {[type]} [description]
 */
MapBase.prototype.fixMapLayout = function(){
	this.mapObj.panBy(1, 0);
};

MapBase.prototype.getMaxZoom = function() {
    return 18;
};

MapBase.prototype.getMinZoom = function() {
    return 0;
};

MapBase.prototype.getZoom = function() {
    return this.mapObj.getZoom();
};

MapBase.prototype.zoom = function(isZoomIn) {
    isZoomIn ? this.mapObj.zoomIn() : this.mapObj.zoomOut();
};

MapBase.prototype.createLine = function (options){
    var strokeColor = options.strokeColor || 'green';
    var strokeStyle = options.strokeStyle || 'solid';
    var strokeOpacity = options.strokeOpacity || 1;
    var strokeWeight = options.strokeWeight || 3;

    var lineArr = new Array();
    lineArr = lineArr.concat(options.path);
    var polyline = new AMap.Polyline({
        path:lineArr,
        strokeColor:strokeColor,
        strokeOpacity:strokeOpacity,
        strokeWeight:strokeWeight,
        strokeStyle:strokeStyle
    });

    return polyline;
}